package com.iemes.controller.purchase;

import java.text.SimpleDateFormat; 
import java.util.Date;
import java.util.List;     
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ApplyFormMap;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.BrandModelFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.MaterFromMap;
import com.iemes.entity.QualityFromMap;
import com.iemes.entity.RoleFormMap;
import com.iemes.entity.SuppFilleFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.entity.PurchaseFromMap;
import com.iemes.entity.WhetherpassFromMap;
import com.iemes.mapper.ApplyMapper;
import com.iemes.mapper.DepartmentMapper;
import com.iemes.mapper.ResourcesMapper;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.material.PurchaseMapper;
import com.iemes.mapper.material.QualityMapper;
import com.iemes.mapper.material.WhetherpassMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


@RequestMapping("/purcha/")
@Controller
public class PurchaController extends BaseController {
	@Inject
	private SuppfilleMapper suppMapper;
	@Inject
	private PurchaseMapper purMapper;
	@Inject
	private ResourcesMapper roleMapper;
	@Inject
	private QualityMapper qualityMapper;
	@Inject
	private UserMapper userMapper;
	@Inject
	private WhetherpassMapper passMapper;
	
	@RequestMapping("examinepurde")
	public String examinepru(Model model){
		List<PurchaseFromMap> purFormMaps=purMapper.findByWhere(new PurchaseFromMap());
		model.addAttribute("pur", purFormMaps);
		
		List<UserFormMap> userFormMaps=userMapper.findByWhere(new UserFormMap());
		model.addAttribute("alluser", userFormMaps);
		
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		
		List<BrandFormMap> brandFormMaps=userMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brand", brandFormMaps);
		
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("quality", qualityFormMap);
		
		/*部门*/
		List<DepartmentForMap> depFormMaps=userMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("dep", depFormMaps);
		
		
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/material_pur/material_pur_list";
	}
	
	@ResponseBody
	@RequestMapping("purcha_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		PurchaseFromMap purchaseFromMap = getFormMap(PurchaseFromMap.class);
		List<Map<String, Object>> purFormMaps=purMapper.getAllName(purchaseFromMap);
		LuogicPaging(pageNow, pageSize, purFormMaps);
		pageView.setRecords(purFormMaps);
		return pageView;
	}
	
	
	/**
	 * 采购列表
	 * @param model
	 * @return
	 */
	@RequestMapping("buyInfo")
	public String buyInfo(Model model){
		List<PurchaseFromMap> purFormMaps=purMapper.findByWhere(new PurchaseFromMap());
		model.addAttribute("pur", purFormMaps);
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		List<BrandFormMap> brandFormMaps=userMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brand", brandFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		List<WhetherpassFromMap> passFormMap=passMapper.findByWhere(new WhetherpassFromMap());
		model.addAttribute("pass", passFormMap);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/material_pur/material_pur_info";
	}
	
	/**
	 * 采购列表
	 * @param pageNow
	 * @param pageSize
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("buy_findByPage")
	public PageView findByPages(String pageNow, String pageSize) throws Exception 
	{
		PurchaseFromMap purchaseFromMap = getFormMap(PurchaseFromMap.class);
		List<Map<String, Object>> purFormMaps=purMapper.selectAll(purchaseFromMap);
		LuogicPaging(pageNow, pageSize, purFormMaps);
		pageView.setRecords(purFormMaps);
		return pageView;
	}
	
	
	//编辑订单
		@RequestMapping("pur_edit")
		public String editUI(Model model) throws Exception 
		{
			String id = getPara("id");
			//获取所有客户
			List<PurchaseFromMap> purFormMap=purMapper.findByWhere(new PurchaseFromMap());
			model.addAttribute("pur", purFormMap);
			List<DepartmentForMap> depFormMaps=userMapper.findByWhere(new DepartmentForMap());
			model.addAttribute("dep", depFormMaps);
			if(Common.isNotEmpty(id)){
				PurchaseFromMap purCheckFormMap=baseMapper.findbyFrist("id", id, PurchaseFromMap.class);
				model.addAttribute("before",purCheckFormMap);
				SuppFilleFromMap suppFormMaps= getFormMap(SuppFilleFromMap.class);
				suppFormMaps.put("purchaseNo", purCheckFormMap.getStr("purchaseNo"));
				List<SuppFilleFromMap> suppFormMap=baseMapper.findByNames(suppFormMaps);
				model.addAttribute("supp", suppFormMap);
			}
			
			return Common.BACKGROUND_PATH + "/material_pur/material_pur_edit";
		}

	
	@ResponseBody
	@RequestMapping(value="pur_editEntity")
	@SystemLog(module="物料管理",methods="采购单-修改表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		PurchaseFromMap purCheckFormMap=getFormMap(PurchaseFromMap.class);
		baseMapper.editEntity(purCheckFormMap);
		//先删除
		baseMapper.deleteByAttribute("purchaseNo","'"+purCheckFormMap.getStr("purchaseNo")+"'", SuppFilleFromMap.class);
		//再添加
		String[] suppName = request.getParameterValues("perpor1");
		String[] materialNo = request.getParameterValues("perpor2");
		String[] pcount =request.getParameterValues("perpor3"); 
		String[] newprice =request.getParameterValues("perpor4");
		String[] suppNumber =request.getParameterValues("perpor5");
		String[] purchaseNo =request.getParameterValues("perpor6");
		if(null != materialNo ){
			for (int i = 0; i < materialNo.length; i++) {
				SuppFilleFromMap suppFormMap1 = new SuppFilleFromMap();
				suppFormMap1.put("purchaseNo", purchaseNo[i]);
				suppFormMap1.put("suppNumber", suppNumber[i]);
				suppFormMap1.put("suppName", suppName[i]);
				suppFormMap1.put("materialNo", materialNo[i]);
				suppFormMap1.put("pcount", pcount[i]);
				suppFormMap1.put("newprice", newprice[i]);
				baseMapper.addEntity(suppFormMap1);
				
			}
		}
		return "success";
	}

	@RequestMapping("pur_addUI")
	public String addUI(Model model) throws Exception {
		List<DepartmentForMap> depFormMaps=userMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("dep", depFormMaps);
		
		return Common.BACKGROUND_PATH + "/material_pur/material_pur_add";
	}
	@ResponseBody
	@RequestMapping("pur_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="供应商-新增物料清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		PurchaseFromMap purchaseFromMap = getFormMap(PurchaseFromMap.class);
		baseMapper.addEntity(purchaseFromMap);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
 		//再添加
		String[] suppNumber =request.getParameterValues("perpor1");
		String[] suppName = request.getParameterValues("perpor2");
		String[] purchaseNo =request.getParameterValues("perpor3");
		String[] materialNo = request.getParameterValues("perpor4");
		String[] newprice =request.getParameterValues("perpor5");
		String[] pcount =request.getParameterValues("perpor6"); 
		if(null != materialNo ){
			for (int i = 0; i < materialNo.length; i++) {
				SuppFilleFromMap suppFormMap1 = new SuppFilleFromMap();
				suppFormMap1.put("purchaseNo", purchaseNo[i]);
				suppFormMap1.put("suppNumber", suppNumber[i]);
				suppFormMap1.put("suppName", suppName[i]);
				suppFormMap1.put("materialNo", materialNo[i]);
				suppFormMap1.put("pcount", pcount[i]);
				suppFormMap1.put("newprice", newprice[i]);
				baseMapper.addEntity(suppFormMap1);
				
				
			}
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping("pur_returnEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="采购订单-删除采购清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String returnEntity() throws Exception {
		PurchaseFromMap purFormMap=getFormMap(PurchaseFromMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for(int i=0;i<str.length;i++){
			purFormMap.put("date", sdf.format(date));
			purFormMap.set("applyId", 2);
			purFormMap.set("whetherpass", 0);
			purFormMap.put("id", str[i]);
			suppMapper.editEntity(purFormMap);
		}
		return "success";
	}
	
	//删除采购单
	@ResponseBody
	@RequestMapping("pur_delEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="采购订单-删除采购清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		PurchaseFromMap purFormMap=getFormMap(PurchaseFromMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for(int i=0;i<str.length;i++){
			purFormMap.put("date", sdf.format(date));
			purFormMap.set("status", 0);
			purFormMap.put("id", str[i]);
			suppMapper.editEntity(purFormMap);
		}
		return "success";
	}
}

